package cn.myzf.tplink.impl.tplink;



import java.io.IOException;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by kevin on 2017/2/21.
 */

public class WR842NRequestor {


    private static OkHttpClient mOkHttpClient = new OkHttpClient();
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    public static String request(String ip, String url, String cookie) {
        url = "http://" + ip + url;

        String cookieTemp = "Authorization=" + cookie + "; ChgPwdSubTag=";
        String referer = "http://" + ip ;
        String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2)"
                + " AppleWebKit/537.36 (KHTML, like Gecko)"
                + " Chrome/55.0.2883.95 Safari/537.36";

        RequestBody body = RequestBody.create(JSON, "{\"hosts_info\":{\"table\":\"host_info\"},\"method\":\"get\"}");
        Request request = new Request.Builder()
                .addHeader("Cookie", cookieTemp)
                .addHeader("Referer", referer)
                .addHeader("User-Agent", userAgent)
                .post(body)
                .url("http://192.168.0.1/stok=cdb09b4325028cdd17dc3e017da6eb6b/ds")
                .build();

//        {"hosts_info":{"table":"host_info"},"method":"get"}

        String html = null;
        okhttp3.Response response = null;
        try {
            response = mOkHttpClient.newCall(request).execute();

            if (response.isSuccessful()) {
                html =  response.body().string();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return html;
    }

}
